<?php

/**
 * Lev.levme.com [ 专业开发各种Discuz!插件 ]
 *
 * Copyright (c) 2013-2014 http://www.levme.com All rights reserved.
 *
 * Author: Mr.Lee <675049572@qq.com>
 *
 * Date: 2013-02-17 16:22:17 Mr.Lee $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

require_once 'lev_module.php';

class lev_class extends lev_module {
	
	public static function _outbank() {
		global $_G;
		$uid = $_G['uid'] >0 ? $_G['uid'] : exit('-5');
		$_groups = unserialize(self::$PL_G['groups']);
		if (!$_groups || !in_array($_G['groupid'], $_groups)) showmessage(self::$lang['errtx']);
		$res = DB::fetch_first("SELECT * FROM ".DB::table('lev_bank_user')." WHERE uid='$uid'");
		if (empty($res)) showmessage(self::$lang['error'].' noservices');
		if ($res['password'] !=md5(md5($_GET['password']))) showmessage(self::$lang['error'].self::$lang['errpwd']);
		
		$bank  = intval($_GET['bank']);
		$settings = (array)unserialize($res['settings']);
		if (empty($settings['bankinfo']) || empty($settings['bankinfo'][$bank])) showmessage(self::$lang['error'].' errbank');
		
		$banks = self::banks();
		if (!$banks[$bank]) showmessage(self::$lang['error'].'bankno');
		if (!$settings['bankinfo'][$bank]) showmessage(self::$lang['error'].' nosetbank');
		
		$money = is_numeric($_GET['money']) && $_GET['money'] >0 ? $_GET['money'] : showmessage(self::$lang['error'].self::$lang['errmoney']);
		$isper = self::isper($money);
		$duiha = self::duihuan();
		$score = ceil(($money + $isper) * $duiha);
		$istx  = self::acscore(-$score, self::$lang['regtx']);
		if ($istx) {
			$bankinfo = $settings['bankinfo'][$bank];
			$bankinfo['bankname'] = $banks[$bank][1];
			$bankinfo['rname'] = $res['rname'];
			$inserts = array(
				'uid'     => $_G['uid'],
				'bbsname' => $_G['username'],
				'codenum' => date('YmdHis', TIMESTAMP).random(5, 1),
				'bankid'  => $bank,
				'money'   => $money,
				'per'     => $isper,
				'bankinfo' => serialize($bankinfo),
				'addtime'  => TIMESTAMP,
			);
			DB::insert('lev_bank_log', $inserts);
			dheader('Location:home.php?mod=spacecp&ac=plugin&op=credit&id=levbank:levbank&m=9&bid='.$bank);
			//showmessage(self::$lang['succeed'].self::$lang['suctxtips']);
		}else {
			showmessage(self::$lang['error'].self::$lang['txtips']);
		}
	}
	public static function isper($money) {
		if ($money < self::$PL_G['minmoney']) showmessage(self::$lang['error'].'minmoney');
		if (self::$PL_G['maxmoney'] >=1 && $money >self::$PL_G['maxmoney']) showmessage(self::$lang['error'].'maxmoney');
		$isper = round($money * self::$PL_G['per'] /100, 2);
		if ($isper < self::$PL_G['minper']) {
			$isper = self::$PL_G['minper'];
		}elseif (self::$PL_G['maxper'] >0 && $isper > self::$PL_G['maxper']) {
			$isper = self::$PL_G['maxper'];
		}
		return $isper;
	}
	
	public static function banks() {
		$_banks  = array();
		$__banks = explode("\n", trim(self::$PL_G['banks']));
		
		foreach ($__banks as $__r) {
			$__r = trim($__r);
			if ($__r) {
				$__one = explode('==', $__r);
				if ($__one[3] !=1)	$_banks[$__one[0]] = $__one;
			}
		}
		return $_banks;
	}
	
	public static function _setbank() {
		global $_G;
		$uid   = $_G['uid'] >0 ? $_G['uid'] : showmessage('Login...', '', '', array('login'=>TRUE));
		$info  = $_GET['setting'];
		if (!$info['cardnum'] || $info['cardnum'] !=$_GET['cardnum2']) showmessage(self::$lang['error'].' cardnum');
		$bank  = intval($info['bankid']);
		$banks = self::banks();
		if (!$banks[$bank]) showmessage(self::$lang['error'].'bankno');
		$res = DB::fetch_first("SELECT * FROM ".DB::table('lev_bank_user')." WHERE uid='$uid'");
		if (empty($res)) showmessage(self::$lang['error'].' nouid');
		if ($res['password'] != md5(md5($_GET['password']))) showmessage(self::$lang['error'].self::$lang['errpwd']);
		$settings = (array)unserialize($res['settings']);
		$settings['bankinfo'][$bank] = array('bankid'=>$bank, 'cardnum'=>$info['cardnum']);
		DB::update('lev_bank_user', array('settings'=>serialize($settings)), array('id'=>$res['id']));
		showmessage(self::$lang['succeed'], dreferer());
	}
	
	public static function _regbank() {
		global $_G;
		$rname = ($_GET['rname'] && addslashes($_GET['rname']) == $_GET['rname']) ? $_GET['rname'] : showmessage(self::$lang['error'].' err name');
		$pwd = trim($_GET['password']);
		if (!$pwd || strlen($pwd) <6 || $pwd != trim($_GET['password2'])) {
			showmessage(self::$lang['error'].' err password2');
		}
		$rs = DB::fetch_first("SELECT * FROM ".DB::table('lev_bank_user')." WHERE uid={$_G['uid']}");
		if ($rs) {
			if (!$_GET['oldpwd'] || md5(md5($_GET['oldpwd'])) !=$rs['password']) showmessage(self::$lang['error'].self::$lang['oldpwd']);
			DB::update('lev_bank_user', 
				array(
					'rname'   => $rname, 
					'mobile'  => strip_tags($_GET['mobile']), 
					'qq'      => strip_tags($_GET['qq']), 
					'password'=> md5(md5($pwd)), 
					'uptime'  => TIMESTAMP
				),
				array('id'=>$rs['id'])
			);
		}else {
			$res = DB::fetch_first("SELECT * FROM ".DB::table('ucenter_members')." WHERE uid='$_G[uid]'");
			if (empty($res)) showmessage(self::$lang['error'].' nouid');
			$password = md5(md5($_GET['loginpwd']).$res['salt']);
			if ($password != $res['password']) showmessage(self::$lang['error'].self::$lang['errpwdbbs']);
			DB::insert('lev_bank_user', 
				array(
					'uid'     => $_G['uid'], 
					'bbsname' => $_G['username'], 
					'rname'   => $rname, 
					'mobile'  => strip_tags($_GET['mobile']), 
					'qq'      => strip_tags($_GET['qq']), 
					'password'=> md5(md5($pwd)), 
					'uptime'  => TIMESTAMP, 
					'addtime' => TIMESTAMP
				)
			);
		}
		showmessage(self::$lang['succeed'], dreferer());
	}
	
	public static function duihuan() {
		$duihuan = intval(self::$PL_G['duihuan']) >0 ? intval(self::$PL_G['duihuan']) : 1;
		return $duihuan;
	}
	
	
}













